﻿using Comm;
using Kinlo.Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Repo.MySql
{

    public class BeforWeightFirstRep
    {
        string strConn; 
        public BeforWeightFirstRep()
        {
            strConn = System.Configuration.ConfigurationManager.ConnectionStrings["WeightDBMySql"].ConnectionString; 
        }
        public int Add2(BeforWeightFirst w)
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {
                    var cmd = @"INSERT BeforWeight_First(ID,Code ,BeforWeight_Index ,BeforWeight_First ,ProductDate,Weight_OK,IsValidate,`Index`,PoleEarOK)
                        VALUES(@ID,@Code, @BeforWeight_Index, @BeforWeight_First, @ProductDate,@Weight_OK,@IsValidate,@Index,@PoleEarOK)";

                    var sqlCmd = new MySqlCommand(cmd, conn);
                    sqlCmd.Parameters.Add(new MySqlParameter("@ID", w.ID == null ? DBNull.Value : (object)w.ID));
                    sqlCmd.Parameters.Add(new MySqlParameter("@Code", w.Code == null ? DBNull.Value : (object)w.Code));
                    sqlCmd.Parameters.Add(new MySqlParameter("@IsValidate", w.IsValidate == null ? DBNull.Value : (object)w.IsValidate));
                    sqlCmd.Parameters.Add(new MySqlParameter("@BeforWeight_Index", w.BeforWeight_Index == null ? DBNull.Value : (object)w.BeforWeight_Index));
                    sqlCmd.Parameters.Add(new MySqlParameter("@BeforWeight_First", w.BeforWeight_First == null ? DBNull.Value : (object)w.BeforWeight_First));
                    sqlCmd.Parameters.Add(new MySqlParameter("@ProductDate", w.ProductDate == null ? DBNull.Value : (object)w.ProductDate));
                    sqlCmd.Parameters.Add(new MySqlParameter("@Weight_OK", w.Weight_OK == null ? DBNull.Value : (object)w.Weight_OK));
                    sqlCmd.Parameters.Add(new MySqlParameter("@Index", w.Index == null ? DBNull.Value : (object)w.Index));
                    sqlCmd.Parameters.Add(new MySqlParameter("@PoleEarOK", w.PoleEarOK == null ? DBNull.Value : (object)w.PoleEarOK));

                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "BeforWeightFirstRep", "Add2", ex);
                return 0;
            }
        }

        /// <summary>
        /// 更新状态 是否已失效
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public int Update(string code)
        {
            var cmd = "";
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {

                    cmd = string.Format(@"UPDATE BeforWeight_First SET IsValidate=0 WHERE Code='{0}'", code);
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2} {3}", "BeforWeightFirstRep", "Update", cmd, ex);
                return 0;
            }
        }
        

        public IList<BeforWeightFirst> GetBeforWeight1()
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {
                    var cmd = @"SELECT  * FROM BeforWeight_First WHERE IsValidate=1 AND	ProductDate > DATE_ADD(NOW(),INTERVAL -3 DAY) ORDER BY ProductDate DESC LIMIT 1000";
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    var BeforWeightFirsts = sqlCmd.ExecuteReader().ReaderToList<BeforWeightFirst>();// 
                    if (BeforWeightFirsts.Count > 0)
                        return BeforWeightFirsts;
                }
                return null;
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "BeforWeightFirstRep", "GetBeforWeight1", ex);
                return null;
            }
        }

        public int DeleteBeforWeight(BeforWeightFirst w)
        {
            try
            {
                using (var conn = new MySqlConnection(strConn))
                {

                    var cmd = string.Format(@"DELETE from BeforWeight_First WHERE Code='{0}'", w.Code);
                    var sqlCmd = new MySqlCommand(cmd, conn);
                    conn.Open();
                    return sqlCmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                Logger.Log.ErrorFormat("{0} {1} {2}", "BeforWeightFirstRep", "DeleteBeforWeight", ex);
                return 0;
            }
        }
    }

}
